package com.zoweunion.mechanic.controller.base;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.base.EnclosureService;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description: 电子围栏
 * @author: skyxu
 * @date: 2019/2/11 0011 14:16
 * @Copyright: 2019 www.zoweunion.com Inc. All rights reserved.
 */
@RestController
@RequestMapping("/enclosure")
public class EnclosureController extends BaseController {

    @Autowired
    private EnclosureService enclosureService;

    /**
     *  获取服务商下的用户 r_id in (1, 7, 8)
     */
    @PostMapping("/getServerAllUser")
    public ResponseBean getServerAllUser(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取服务商下的用户 r_id in (1, 7, 8)");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        reqMap.put("s_id",getCurrentUser(request).getS_id());
        reqMap.put("current_login_user_id",getCurrentUser(request).getId());
        Map<String, Object> resultMap=enclosureService.getServerAllUser(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }


    /**
     *  添加电子围栏
     *   radius: 围栏半径 lnglat：围栏经纬度
     */
    @PostMapping("/addEnclosure")
    public ResponseBean addEnclosure(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "电子围栏_添加电子围栏");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User user = getCurrentUser(request);
        reqMap.put("s_id",user.getS_id());
        reqMap.put("u_id",user.getId());
        reqMap.put("area",user.getArea());
        int result = enclosureService.addEnclosure(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",result);
    }


    /**
     *  获取电子围栏
     *   radius: 围栏半径 lnglat：围栏经纬度
     */
    @PostMapping("/getEnclosureList")
    public ResponseBean getEnclosureList(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "电子围栏_获取电子围栏");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User user = getCurrentUser(request);
        reqMap.put("s_id", user.getS_id());
        if(reqMap.get("server_type") == null || !(Integer.valueOf(reqMap.get("server_type").toString()) == 4)) {
            reqMap.put("area", user.getArea());
        }
        Map<String, Object> resultMap=enclosureService.getEnclosureList(reqMap);
        if(resultMap==null){
            return new ResponseBean(Constants.CODE_SUCCESS,"无数据",null);
        }
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }

    /**
     * 获取电子围栏的详情
     * @param str
     * @return
     * @throws MyException
     */
    @PostMapping("/getEnclosureInfo")
    public ResponseBean getEnclosureInfo(@RequestBody String str) throws MyException {
        logBefore(logger, "电子围栏_获取电子围栏的详情");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        Map<String, Object> resultMap = enclosureService.getEnclosureInfo(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
    }

    /**
     *  删除电子围栏
     *
     */
    @PostMapping("/delEnclosureList")
    public ResponseBean delEnclosureList(@RequestBody String str) throws MyException {
        logBefore(logger, "电子围栏_删除电子围栏");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        int result = enclosureService.delEnclosureList(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",result);
    }

}
